<?php
//ini_set('display_errors', 'On');
require_once 'base.php';
checkAuth();

$op = Utility::getParam('op', 'manage');
switch($op) {
	case 'add':
		$op();
		break;
	case 'doAdd':
		$op();
		break;
	case 'modify':
		$op();
		break;
	case 'doModify':
		$op();
		break;
	case 'doDelete':
		$op();
		break;
	case 'manage':
		$op();
		break;
	default:
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		break;
}

function add() {
	
	$conf = $GLOBALS['conf'];
	include $GLOBALS['ROOT_PATH'] . '/views/admin/express_add.phtml';
}

function doAdd() {
	
	$name = Utility::getParam('cname', '');
    $tel = Utility::getParam('tel', '');
    $remark = Utility::getParam('remark', '');
    
    if (empty($name) || empty($tel)){
    	echoAjax(Constants::$ERR_MISSING_PARAMS);
    	return;
    }
    if (ObjectFinder::get('Express')->getInfoByName($name)) {
    	echoAjax(Constants::$ERR_ADD_FAILED_FOR_EXPRESS_EXIST);
    	return ;
    }
    
    $res = (int)ObjectFinder::get('Express')->add(array(
    	'name'	=>	$name,
    	'tel'	=>	$tel,
    	'remark'	=>	$remark
    ));
    echoAjax($res ? Constants::$ERR_SUCCESS : Constants::$ERR_FAILED);
}

function modify() {
	
	$conf = $GLOBALS['conf'];
	$id = max(0, (int)Utility::getParam('id', 0));
    if (empty($id)){
    	echoAjax(Constants::$ERR_INVALID_PARAMS);
    	return ;
    }
    $info = ObjectFinder::get('Express')->getInfoById($id);
    
    include $GLOBALS['ROOT_PATH'] . '/views/admin/express_modify.phtml';
}

function doModify() {
	
	$id = (int)Utility::getParam('id', 0);
	$name = Utility::getParam('cname', '');
    $tel = Utility::getParam('tel', '');
    $remark = Utility::getParam('remark', '');
	if (empty($id) || empty($name) || empty($tel)){
    	echoAjax(Constants::$ERR_MISSING_PARAMS);
    	return;
    }
    
    ObjectFinder::get('Express')->modify(array('name'=>$name, 'tel'=>$tel, 'remark'=>$remark), array('id'=>$id));
    echoAjax(Constants::$ERR_SUCCESS);
}

function doDelete() {
	$id = (int)Utility::getParam('id', 0);
    if (empty($id)) {
    	echoAjax(Constants::$ERR_MISSING_PARAMS);
    	return;	
    }
    $res = ObjectFinder::get('Express')->delById($id);
    echoAjax($res ? Constants::$ERR_SUCCESS : Constants::$ERR_FAILED);
}

function manage() {
	$conf = $GLOBALS['conf'];
	$start = (int)Utility::getParam('start', 0);
    $page = (int)Utility::getParam('page', 1);
    $page = max(1, $page);
    $count = $page * $conf['pageSize'];
    $list = ObjectFinder::get('Express')->getList($start, $count);
    include $GLOBALS['ROOT_PATH'] . '/views/admin/express_manage.phtml';
}
?>
